home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / dos / c / des_c.exe / lha / DES.C < prev    next >
Text File  |  1990-07-14  |  14KB  |  528 lines

  1. /* Sofware DES functions
  2.  * written 12 Dec 1986 by Phil Karn, KA9Q; large sections adapted from
  3.  * the 1977 public-domain program by Jim Gillogly
  4.  */
  5.  
  6. #ifdef __TURBOC__
  7. #include <stdio.h>
  8. #include <alloc.h>
  9. #define LITTLE_ENDIAN
  10. #else
  11. #define    NULL    0
  12. #endif
  13.  
  14. #ifdef    LITTLE_ENDIAN
  15. unsigned long byteswap();
  16. #endif
  17.  
  18. /* Tables defined in the Data Encryption Standard documents */
  19.  
  20. /* initial permutation IP */
  21. static char ip[] = {
  22.     58, 50, 42, 34, 26, 18, 10,  2,
  23.     60, 52, 44, 36, 28, 20, 12,  4,
  24.     62, 54, 46, 38, 30, 22, 14,  6,
  25.     64, 56, 48, 40, 32, 24, 16,  8,
  26.     57, 49, 41, 33, 25, 17,  9,  1,
  27.     59, 51, 43, 35, 27, 19, 11,  3,
  28.     61, 53, 45, 37, 29, 21, 13,  5,
  29.     63, 55, 47, 39, 31, 23, 15,  7
  30. };
  31.  
  32. /* final permutation IP^-1 */
  33. static char fp[] = {
  34.     40,  8, 48, 16, 56, 24, 64, 32,
  35.     39,  7, 47, 15, 55, 23, 63, 31,
  36.     38,  6, 46, 14, 54, 22, 62, 30,
  37.     37,  5, 45, 13, 53, 21, 61, 29,
  38.     36,  4, 44, 12, 52, 20, 60, 28,
  39.     35,  3, 43, 11, 51, 19, 59, 27,
  40.     34,  2, 42, 10, 50, 18, 58, 26,
  41.     33,  1, 41,  9, 49, 17, 57, 25
  42. };
  43.  
  44. /* expansion operation matrix
  45.  * This is for reference only; it is unused in the code
  46.  * as the f() function performs it implicitly for speed
  47.  */
  48. #ifdef notdef
  49. static char ei[] = {
  50.     32,  1,  2,  3,  4,  5,
  51.      4,  5,  6,  7,  8,  9,
  52.      8,  9, 10, 11, 12, 13,
  53.     12, 13, 14, 15, 16, 17,
  54.     16, 17, 18, 19, 20, 21,
  55.     20, 21, 22, 23, 24, 25,
  56.     24, 25, 26, 27, 28, 29,
  57.     28, 29, 30, 31, 32,  1 
  58. };
  59. #endif
  60.  
  61. /* permuted choice table (key) */
  62. static char pc1[] = {
  63.     57, 49, 41, 33, 25, 17,  9,
  64.      1, 58, 50, 42, 34, 26, 18,
  65.     10,  2, 59, 51, 43, 35, 27,
  66.     19, 11,  3, 60, 52, 44, 36,
  67.  
  68.     63, 55, 47, 39, 31, 23, 15,
  69.      7, 62, 54, 46, 38, 30, 22,
  70.     14,  6, 61, 53, 45, 37, 29,
  71.     21, 13,  5, 28, 20, 12,  4
  72. };
  73.  
  74. /* number left rotations of pc1 */
  75. static char totrot[] = {
  76.     1,2,4,6,8,10,12,14,15,17,19,21,23,25,27,28
  77. };
  78.  
  79. /* permuted choice key (table) */
  80. static char pc2[] = {
  81.     14, 17, 11, 24,  1,  5,
  82.      3, 28, 15,  6, 21, 10,
  83.     23, 19, 12,  4, 26,  8,
  84.     16,  7, 27, 20, 13,  2,
  85.     41, 52, 31, 37, 47, 55,
  86.     30, 40, 51, 45, 33, 48,
  87.     44, 49, 39, 56, 34, 53,
  88.     46, 42, 50, 36, 29, 32
  89. };
  90.  
  91. /* The (in)famous S-boxes */
  92. static char si[8][64] = {
  93.     /* S1 */
  94.     14,  4, 13,  1,  2, 15, 11,  8,  3, 10,  6, 12,  5,  9,  0,  7,
  95.      0, 15,  7,  4, 14,  2, 13,  1, 10,  6, 12, 11,  9,  5,  3,  8,
  96.      4,  1, 14,  8, 13,  6,  2, 11, 15, 12,  9,  7,  3, 10,  5,  0,
  97.     15, 12,  8,  2,  4,  9,  1,  7,  5, 11,  3, 14, 10,  0,  6, 13,
  98.  
  99.     /* S2 */
  100.     15,  1,  8, 14,  6, 11,  3,  4,  9,  7,  2, 13, 12,  0,  5, 10,
  101.      3, 13,  4,  7, 15,  2,  8, 14, 12,  0,  1, 10,  6,  9, 11,  5,
  102.      0, 14,  7, 11, 10,  4, 13,  1,  5,  8, 12,  6,  9,  3,  2, 15,
  103.     13,  8, 10,  1,  3, 15,  4,  2, 11,  6,  7, 12,  0,  5, 14,  9,
  104.  
  105.     /* S3 */
  106.     10,  0,  9, 14,  6,  3, 15,  5,  1, 13, 12,  7, 11,  4,  2,  8,
  107.     13,  7,  0,  9,  3,  4,  6, 10,  2,  8,  5, 14, 12, 11, 15,  1,
  108.     13,  6,  4,  9,  8, 15,  3,  0, 11,  1,  2, 12,  5, 10, 14,  7,
  109.      1, 10, 13,  0,  6,  9,  8,  7,  4, 15, 14,  3, 11,  5,  2, 12,
  110.  
  111.     /* S4 */
  112.      7, 13, 14,  3,  0,  6,  9, 10,  1,  2,  8,  5, 11, 12,  4, 15,
  113.     13,  8, 11,  5,  6, 15,  0,  3,  4,  7,  2, 12,  1, 10, 14,  9,
  114.     10,  6,  9,  0, 12, 11,  7, 13, 15,  1,  3, 14,  5,  2,  8,  4,
  115.      3, 15,  0,  6, 10,  1, 13,  8,  9,  4,  5, 11, 12,  7,  2, 14,
  116.  
  117.     /* S5 */
  118.      2, 12,  4,  1,  7, 10, 11,  6,  8,  5,  3, 15, 13,  0, 14,  9,
  119.     14, 11,  2, 12,  4,  7, 13,  1,  5,  0, 15, 10,  3,  9,  8,  6,
  120.      4,  2,  1, 11, 10, 13,  7,  8, 15,  9, 12,  5,  6,  3,  0, 14,
  121.     11,  8, 12,  7,  1, 14,  2, 13,  6, 15,  0,  9, 10,  4,  5,  3,
  122.  
  123.     /* S6 */
  124.     12,  1, 10, 15,  9,  2,  6,  8,  0, 13,  3,  4, 14,  7,  5, 11,
  125.     10, 15,  4,  2,  7, 12,  9,  5,  6,  1, 13, 14,  0, 11,  3,  8,
  126.      9, 14, 15,  5,  2,  8, 12,  3,  7,  0,  4, 10,  1, 13, 11,  6,
  127.      4,  3,  2, 12,  9,  5, 15, 10, 11, 14,  1,  7,  6,  0,  8, 13,
  128.  
  129.     /* S7 */
  130.      4, 11,  2, 14, 15,  0,  8, 13,  3, 12,  9,  7,  5, 10,  6,  1,
  131.     13,  0, 11,  7,  4,  9,  1, 10, 14,  3,  5, 12,  2, 15,  8,  6,
  132.      1,  4, 11, 13, 12,  3,  7, 14, 10, 15,  6,  8,  0,  5,  9,  2,
  133.      6, 11, 13,  8,  1,  4, 10,  7,  9,  5,  0, 15, 14,  2,  3, 12,
  134.  
  135.     /* S8 */
  136.     13,  2,  8,  4,  6, 15, 11,  1, 10,  9,  3, 14,  5,  0, 12,  7,
  137.      1, 15, 13,  8, 10,  3,  7,  4, 12,  5,  6, 11,  0, 14,  9,  2,
  138.      7, 11,  4,  1,  9, 12, 14,  2,  0,  6, 10, 13, 15,  3,  5,  8,
  139.      2,  1, 14,  7,  4, 10,  8, 13, 15, 12,  9,  0,  3,  5,  6, 11
  140. };
  141.  
  142. /* 32-bit permutation function P used on the output of the S-boxes */
  143. static char p32i[] = {    
  144.     16,  7, 20, 21,
  145.     29, 12, 28, 17,
  146.      1, 15, 23, 26,
  147.      5, 18, 31, 10,
  148.      2,  8, 24, 14,
  149.     32, 27,  3,  9,
  150.     19, 13, 30,  6,
  151.     22, 11,  4, 25
  152. };
  153. /* End of DES-defined tables */
  154.  
  155. /* Lookup tables initialized once only at startup by desinit() */
  156. static long (*sp)[64];        /* Combined S and P boxes */
  157.  
  158. static char (*iperm)[16][8];    /* Initial and final permutations */
  159. static char (*fperm)[16][8];
  160.  
  161. /* 8 6-bit subkeys for each of 16 rounds, initialized by setkey() */
  162. static unsigned char (*kn)[8];
  163.  
  164. /* bit 0 is left-most in byte */
  165. static int bytebit[] = {
  166.     0200,0100,040,020,010,04,02,01
  167. };
  168.  
  169. static int nibblebit[] = {
  170.      010,04,02,01
  171. };
  172. static int desmode;
  173.  
  174. /* Allocate space and initialize DES lookup arrays
  175.  * mode == 0: standard Data Encryption Algorithm
  176.  * mode == 1: DEA without initial and final permutations for speed
  177.  * mode == 2: DEA without permutations and with 128-byte key (completely
  178.  *            independent subkeys for each round)
  179.  */
  180. desinit(mode)
  181. int mode;
  182. {
  183. #ifndef __TURBOC__
  184.     char *malloc();
  185. #endif
  186.  
  187.     if(sp != NULL){
  188.         /* Already initialized */
  189.         return 0;
  190.     }
  191.     desmode = mode;
  192.     
  193.     if((sp = (long (*)[64])malloc(sizeof(long) * 8 * 64)) == NULL){
  194.         return -1;
  195.     }
  196.     spinit();
  197.     kn = (unsigned char (*)[8])malloc(sizeof(char) * 8 * 16);
  198.     if(kn == NULL){
  199.         free((char *)sp);
  200.         return -1;
  201.     }
  202.     if(mode == 1 || mode == 2)    /* No permutations */
  203.         return 0;
  204.  
  205.     iperm = (char (*)[16][8])malloc(sizeof(char) * 16 * 16 * 8);
  206.     if(iperm == NULL){
  207.         free((char *)sp);
  208.         free((char *)kn);
  209.         return -1;
  210.     }
  211.     perminit(iperm,ip);
  212.  
  213.     fperm = (char (*)[16][8])malloc(sizeof(char) * 16 * 16 * 8);
  214.     if(fperm == NULL){
  215.         free((char *)sp);
  216.         free((char *)kn);
  217.         free((char *)iperm);
  218.         return -1;
  219.     }
  220.     perminit(fperm,fp);
  221.     
  222.     return 0;
  223. }
  224. /* Free up storage used by DES */
  225. desdone()
  226. {
  227.     if(sp == NULL)
  228.         return;    /* Already done */
  229.  
  230.     free((char *)sp);
  231.     free((char *)kn);
  232.     if(iperm != NULL)
  233.         free((char *)iperm);
  234.     if(fperm != NULL)
  235.         free((char *)fperm);
  236.  
  237.     sp = NULL;
  238.     iperm = NULL;
  239.     fperm = NULL;
  240.     kn = NULL;
  241. }
  242. /* Set key (initialize key schedule array) */
  243. setkey(key)
  244. char *key;            /* 64 bits (will use only 56) */
  245. {
  246.     char pc1m[56];        /* place to modify pc1 into */
  247.     char pcr[56];        /* place to rotate pc1 into */
  248.     register int i,j,l;
  249.     int m;
  250.  
  251.     /* In mode 2, the 128 bytes of subkey are set directly from the
  252.      * user's key, allowing him to use completely independent
  253.      * subkeys for each round. Note that the user MUST specify a
  254.      * full 128 bytes.
  255.      *
  256.      * I would like to think that this technique gives the NSA a real
  257.      * headache, but I'm not THAT naive.
  258.      */
  259.     if(desmode == 2){
  260.         for(i=0;i<16;i++)
  261.             for(j=0;j<8;j++)
  262.                 kn[i][j] = *key++;
  263.         return;
  264.     }
  265.     /* Clear key schedule */
  266.     for (i=0; i<16; i++)
  267.         for (j=0; j<8; j++)
  268.             kn[i][j]=0;
  269.  
  270.     for (j=0; j<56; j++) {        /* convert pc1 to bits of key */
  271.         l=pc1[j]-1;        /* integer bit location     */
  272.         m = l & 07;        /* find bit         */
  273.         pc1m[j]=(key[l>>3] &    /* find which key byte l is in */
  274.             bytebit[m])    /* and which bit of that byte */
  275.             ? 1 : 0;    /* and store 1-bit result */
  276.     }
  277.     for (i=0; i<16; i++) {        /* key chunk for each iteration */
  278.         for (j=0; j<56; j++)    /* rotate pc1 the right amount */
  279.             pcr[j] = pc1m[(l=j+totrot[i])<(j<28? 28 : 56) ? l: l-28];
  280.             /* rotate left and right halves independently */
  281.         for (j=0; j<48; j++){    /* select bits individually */
  282.             /* check bit that goes to kn[j] */
  283.             if (pcr[pc2[j]-1]){
  284.                 /* mask it in if it's there */
  285.                 l= j % 6;
  286.                 kn[i][j/6] |= bytebit[l] >> 2;
  287.             }
  288.         }
  289.     }
  290. }
  291. /* In-place encryption of 64-bit block */
  292. endes(block)
  293. char *block;
  294. {
  295.     r